"""
Case Type   : bugfix
Case Name   : select min/max+没用only full group by场景，查询成功
Create At   : 2025/6/12
Owner       : liu-tong-8848
Description :
    1、设置sql_mode为空
    2、建表并插入数据
    3、select min/max+没用only full group by场景
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class BugFix(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbBmode')
        self.user_node = Node('PrimaryDbBmode')
        self.constant = Constant()
        sql_cmd = "show dolphin.sql_mode;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertNotIn("ERROR", sql_res, '执行失败')
        self.dolphin_sql_mode = sql_res.strip().splitlines()[-2]

    def test_dolphin(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '----step1:select min/max+没用only full group by场景； expect:成功----'
        self.log.info(text)
        sql_cmd = "set dolphin.sql_mode = ''; drop table if exists t_fixed_106; " \
                  + "create table t_fixed_106(pk int not null, col_int_nokey int not null, primary key (pk)); " \
                  + "insert into t_fixed_106 values(10, 8); select col_int_nokey, max(pk) from t_fixed_106;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step2:建表并插入数据；expect:成功----'
        self.log.info(text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res, '执行失败' + text)

        text = '----step3:select min/max+没用only full group by场景；expect:成功----'
        self.log.info(text)
        self.assertIn("col_int_nokey", sql_res, '执行失败' + text)
        self.assertIn("max", sql_res, '执行失败' + text)
        self.assertIn("8", sql_res, '执行失败' + text)
        self.assertIn("10", sql_res, '执行失败' + text)
        self.assertIn("(1 row)", sql_res, '执行失败' + text)
        self.assertNotIn("ERROR", sql_res, '执行失败' + text)

    def tearDown(self):
        text = '----step4:清理环境；expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop table if exists t_fixed_106; set dolphin.sql_mode = '{self.dolphin_sql_mode}';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res, '执行失败' + text)
        self.assertIn(self.constant.SET_SUCCESS_MSG, sql_res, '执行失败' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
